package com.weixin.project.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Entity.Favorite;
import com.weixin.project.Entity.Question;
import com.weixin.project.Entity.User;
import com.weixin.project.Service.FavoriteService;
import com.weixin.project.Service.QuestionService;
import com.weixin.project.Service.RecommendService;
import com.weixin.project.Service.UserService;
import com.weixin.project.Util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
public class FavoriteController {
    @Autowired
    UserService userService;
    @Autowired
    FavoriteService favoriteService;
    @Autowired
    QuestionService questionService;
    @Autowired
    RecommendService recommendService;
    @PostMapping("/test/favorite")
    JSONObject addFavorite(HttpServletRequest request,
                           @RequestBody JSONObject params){
        Integer questionId = params.getInteger("id");
        User user = userService.findCurrentUser(request);
        if (favoriteService.findFavorite(questionId, user.getId()) != null) {
            return ResponseUtil.error("该题目已经在收藏夹中");
        }
        Favorite favorite = new Favorite();
        favorite.setCreateTime(new Date());
        favorite.setQuestionId(questionId);
        favorite.setUserId(user.getId());
        favoriteService.storeFavorite(favorite);
        return ResponseUtil.success("收藏成功");
    }

    @PostMapping("/test/cancel_favorite")
    JSONObject cancelFavorite(HttpServletRequest request,
                              @RequestBody JSONObject params){
        User user = userService.findCurrentUser(request);
        Integer questionId = params.getInteger("id");
        Favorite favorite = favoriteService.findFavorite(questionId, user.getId());
        if (favorite == null){
            return ResponseUtil.error("找不到收藏记录");
        }
        favoriteService.deleteFavorite(favorite);
        return ResponseUtil.success();
    }

    @GetMapping("/test/is_favorate")
    JSONObject isFavorite(HttpServletRequest request,
                          @RequestParam("id") Integer questionId){
        JSONObject ret = new JSONObject();
        User user = userService.findCurrentUser(request);
        if(user == null){
            return ResponseUtil.userNotFind();
        }
        Favorite favorite = favoriteService.findFavorite(questionId, user.getId());
        ret.put("is_favorite", favorite != null);
        return ResponseUtil.success("", ret);
    }

    @GetMapping("/user/get_my_favorite")
    JSONObject getFavoritesOfUser(HttpServletRequest request,
                                  @RequestParam("page") Integer page){
        page -- ;
        User user = userService.findCurrentUser(request);
        List<Favorite> favorites = favoriteService.findUserFavorites(user.getId(), page);
        Integer totalPages = favoriteService.countTotalPagesOfUserFavorite(user.getId());
        JSONArray arr = new JSONArray();
        for (Favorite favorite: favorites){
            JSONObject obj = new JSONObject();
            Question question = questionService.findQuestionById(favorite.getQuestionId());
            obj.put("id", question.getId());
            obj.put("content", question.getStem());
            arr.add(obj);
        }
        JSONObject ret = new JSONObject();
        ret.put("counts", favoriteService.countAllFavoritesOfUser(user.getId()));
        ret.put("records", arr);
        ret.put("all_pages", totalPages);
        return ResponseUtil.success("", ret);
    }

}
